package com.hbzhit.modules.sys.entity;


import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.hbzhit.common.entity.ExcelVerifyEntity;
import com.hbzhit.common.utils.CacheUtils;
import com.hbzhit.common.utils.ConfigConstant;
import com.hbzhit.common.utils.Constant;
import com.hbzhit.common.validator.group.ImportGroup;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 系统用户
 *
 * @author DING
 * @date 2019-07-08 10:36:34
 */
@TableName("sys_user")
@ExcelTarget("user")
@Slf4j
@Getter
@Setter
public class SysUserEntity extends ExcelVerifyEntity implements Serializable {
	private static final long serialVersionUID = 922689587736324401L;
	
	/**
	 * 姓名
	 */
	@ApiModelProperty(value="姓名", name="realName")
	@Excel(name = "姓名", width = 20, isImportField = "true_user")
	@NotNull(message = "姓名不能为空", groups = {ImportGroup.class})
	private String realName;
	/**
	 * 手机号
	 */
	@ApiModelProperty(value="联系电话", name="mobile")
	@Excel(name = "联系电话", width = 30)
	private String mobile;
	/**
	 * 用户名
	 */
	@ApiModelProperty(value="用户名", name="username")
	@Excel(name = "用户名", width = 20, isImportField = "true_user")
	@NotNull(message = "用户名不能为空", groups = {ImportGroup.class})
	private String username;
	/**
	 * 密码
	 */
	@ApiModelProperty(value="密码", name="password")
	private String password;

	@ApiModelProperty("密码更新时间")
	private Date pwdUptTime;

	/**
	 * 盐
	 */
	@ApiModelProperty(value="盐", name="salt")
	private String salt;
	/**
	 * 身份证号
	 */
	@ApiModelProperty(value="身份证号", name="idcard")
	@Excel(name = "身份证号", width = 30)
	private String idcard;
	/**
	 * 邮箱
	 */
	@ApiModelProperty(value="邮箱", name="email")
	@Excel(name = "邮箱", width = 30)
	private String email;

	@ApiModelProperty("性别")
	@Excel(name = "性别", replace = { "男_0", "女_1" }, width = 10, isImportField = "true_user")
	@NotNull(message = "性别不能为空", groups = {ImportGroup.class})
	private Integer sex;

	@ApiModelProperty("出生日期")
	@Excel(name = "出生日期", format = "yyyy-MM-dd", width = 30)
	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
	private Date birthDate;

	@ApiModelProperty("政治面貌")
	@Excel(name = "政治面貌", width = 30)
	private String political;

	@ApiModelProperty("文化程度")
	@Excel(name = "文化程度", width = 30)
	private String eduLevel;

	@ApiModelProperty("参工时间")
	@Excel(name = "参工时间", format = "yyyy-MM-dd", width = 30)
	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
	private Date workTime;

	@ApiModelProperty("岗位")
	@Excel(name = "担任职务", width = 30)
	private String job;

	@ApiModelProperty("婚姻状况")
	@Excel(name = "婚姻状况", width = 30)
	private String maritalStatus;

	@ApiModelProperty("是否人员档案")
	private Integer isMember;

	@ApiModelProperty("是否在职")
	private Integer isOnTheJob;


	@ApiModelProperty("加入日期")
	@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
	private Date joinTime;

	/**
	 * 状态  0：禁用   1：正常
	 */
	@ApiModelProperty(value="状态  0：禁用   1：正常", name="status")
	@Excel(name = "状态", replace = { "禁用_0", "正常_1" }, width = 10)
	private Integer status;
	/**
	 * 是否管理用户
	 */
	@ApiModelProperty(value="是否管理用户", name="isAdmin")
	private Integer isAdmin;
	/**
	 * 系统预置
	 */
	@ApiModelProperty(value="系统预置", name="isSys")
	private Integer isSys;
	/**
	 * 最后登录ip
	 */
	@ApiModelProperty(value="最后登录ip", name="lastLoginIp")
	private String lastLoginIp;
	/**
	 * 最后登录时间
	 */
	@ApiModelProperty(value="最后登录时间", name="lastLoginTime")
	private Date lastLoginTime;
	/**
	 * 微信openid
	 */
	@ApiModelProperty(value="微信openid", name="openid")
	@TableField(updateStrategy = FieldStrategy.IGNORED)
	private String openid;

	@ApiModelProperty(value="所属部门ID", name="deptId")
	private Long deptId;

	@ApiModelProperty(value="人员岗位ID", name="jobId")
	private Long jobId;

	@ApiModelProperty("错误次数")
	private Integer wrongTimes;

	@ApiModelProperty("用户角色ID列表")
	@TableField(exist = false)
	private List<Long> roleIdList;

	@ApiModelProperty("密码是否过期")
	@TableField(exist = false)
	private Integer isPwdExpire;

	@ApiModelProperty("部门名称")
	@TableField(exist = false)
	private String deptName;

	@ApiModelProperty("岗位名称")
	@TableField(exist = false)
	private String jobName;

	public Integer getIsPwdExpire() {
		try {
			int expireDay = Integer.parseInt(CacheUtils.getConfCache(ConfigConstant.PASSWORD_EXPIRED).getParamValue());
			if (expireDay == 0 || this.getPwdUptTime() == null) {
				return Constant.INT_N;
			}
			Calendar cal = Calendar.getInstance();
			cal.setTime(this.getPwdUptTime());
			cal.add(Calendar.DATE, expireDay);
			Date pwdExpireDate = cal.getTime();
			if (pwdExpireDate.before(new Date())) {
				return Constant.INT_Y;
			}
		} catch (Exception e) {
			log.warn("判断密码过期时出错，跳过判断。错误详情：", e);
		}
		return Constant.INT_N;
	}

	public String getDeptName () {
		return ObjectUtils.isNotEmpty(this.getDeptId()) ? CacheUtils.getOrgCache(this.getDeptId()).getOrgName() : "";
	}

	public String getJobName () {
		return ObjectUtils.isNotEmpty(this.getJobId()) ? CacheUtils.getOrgCache(this.getJobId()).getOrgName() : "";
	}

	public void setPassword(String password) {
		this.password = password;
		if (StrUtil.isNotBlank(password)) {
			this.pwdUptTime = new Date();
		}
	}

	public List<Long> getRoleIdList() {
		return this.roleIdList == null ? new ArrayList<>() : this.roleIdList;
	}
}
